$(function () {
	$("#jqGrid").Grid({
		url: '../lotteryconfig/list',
		colModel: [
			{label: 'id', name: 'id', index: 'id', key: true, hidden: true},
			{label: '名称', name: 'name', index: 'name', width: 80,align:'center'},
			{label: '图标', name: 'logo', index: 'logo', width: 80,align:'center',
				formatter:function (value) {
					if(value!=null){
						return transImg(value);
					}
					return "";
				}
			},
			{label: 'URL', name: 'url', index: 'url', width: 80,align:'center'},
			{label: '消费数量', name: 'number', index: 'number', width: 80,align:'center'},
			{label: '奖品类型', name: 'type', index: 'type', width: 80,align:'center',
				formatter:function(value){
					if(value==1) {
						return '大转盘活动';
					}else if(value ==2){
						return "幸运转盘";
					}else if(value== 3){
						return "刮刮乐";
					}else if(value==4){
						return "健康打卡";
					}else if(value==5){
						return "周周乐";
					}else {
						return'未定义';
					}
				}
			},
			{label: '优先级', name: 'priority', index: 'priority', width: 80,align:'center'},
			{label: '用户编号逗号分隔', name: 'user', index: 'user', width: 80,align:'center'},
			{label: '中奖级别', name: 'level', index: 'level', width: 80,align:'center'},
			{label: '中奖倍率', name: 'power', index: 'power', width: 80,align:'center'},
			{label: '创建时间', name: 'createDate', index: 'create_date', width: 80,align:'center'},
			{label: '是否开启', name: 'isOpen', index: 'is_open', width: 80,align:'center',
				formatter:function(value){
					if(value==0) {
						return '否';
					}else if(value==1) {
						return '是';
					}
				}
			}]
	});
});

let vm = new Vue({
	el: '#rrapp',
	data: {
		showList: true,
		title: null,
		lotteryConfig: {
			type:''
		},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
			name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.lotteryConfig = {};
		},
		update: function (event) {
			let id = getSelectedRow("#jqGrid");
			if (id == null) {
				return;
			}
			vm.showList = false;
			vm.title = "修改";

			vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			console.log(vm.lotteryConfig.type)
			if(vm.lotteryConfig.type==null||vm.lotteryConfig.type===''){
				iview.Message.error("请选择游戏类型!");
				return;
			}
			let url = vm.lotteryConfig.id == null ? "../lotteryconfig/save" : "../lotteryconfig/update";
			Ajax.request({
				url: url,
				params: JSON.stringify(vm.lotteryConfig),
				type: "POST",
				contentType: "application/json",
				successCallback: function (r) {
					alert('操作成功', function (index) {
						vm.reload();
					});
				}
			});
		},
        opensignbuday: function (event) {
            Ajax.request({
                url: "../lotteryconfig/opensignbyday",
                params: JSON.stringify({}),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        opensignbyweek: function (event) {
            Ajax.request({
                url: "../lotteryconfig/opensignbyweek",
                params: JSON.stringify({}),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
		del: function (event) {
			let ids = getSelectedRows("#jqGrid");
			if (ids == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
				Ajax.request({
					url: "../lotteryconfig/delete",
					params: JSON.stringify(ids),
					type: "POST",
					contentType: "application/json",
					successCallback: function () {
						alert('操作成功', function (index) {
							vm.reload();
						});
					}
				});
			});
		},
		getInfo: function(id){
			Ajax.request({
				url: "../lotteryconfig/info/"+id,
				async: true,
				successCallback: function (r) {
					vm.lotteryConfig = r.lotteryConfig;
				}
			});
		},
		reload: function (event) {
			vm.showList = true;
			let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
				postData: {'name': vm.q.name},
				page: page
			}).trigger("reloadGrid");
			vm.handleReset('formValidate');
		},
		reloadSearch: function() {
			vm.q = {
				name: ''
			}
			vm.reload();
		},
		handleSubmit: function (name) {
			handleSubmitValidate(this, name, function () {
				vm.saveOrUpdate()
			});
		},
		handleReset: function (name) {
			handleResetForm(this, name);
		},handleSuccessLogo: function (res, file) {
			vm.lotteryConfig.logo = file.response.url;
		},
		handleFormatError: function (file) {
			this.$Notice.warning({
				title: '文件格式不正确',
				desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
			});
		},
		handleMaxSize: function (file) {
			this.$Notice.warning({
				title: '超出文件大小限制',
				desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
			});
		}, eyeImageLogo: function () {
			var url = vm.lotteryConfig.logo;
			eyeImage(url);
		}
	}
});